﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class ZL_CP_Edit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lblTitle.Text = "修改产品";
                initValue();
            }
        }

        private void initValue()
        {
            string id = Request.QueryString["id"];
            this.hdId.Value = id;
            ZL_CP_Dao dao = new ZL_CP_Dao();
            ZL_CP zlcp = dao.Query(id);

            // 产品线
            CS_CPXSet_Dao daoCPX = new CS_CPXSet_Dao();
            List<CS_CPX> lsCPX = daoCPX.QueryAll();
            this.ddlCPXBH.DataSource = lsCPX;
            this.ddlCPXBH.DataTextField = "MC";
            this.ddlCPXBH.DataValueField = "BH";
            this.ddlCPXBH.DataBind();
            this.ddlCPXBH.Items.Insert(0, new ListItem("请选择", "0"));
            this.ddlCPXBH.Items.FindByValue(zlcp.CPXBH).Selected = true;

            // 产品类型
            CS_CPLXSet_Dao daoCPLX = new CS_CPLXSet_Dao();
            List<CS_CPLX> lsCPLX = daoCPLX.QueryAll();
            this.ddlCPLXBH.DataSource = lsCPLX;
            this.ddlCPLXBH.DataTextField = "MC";
            this.ddlCPLXBH.DataValueField = "BH";
            this.ddlCPLXBH.DataBind();
            this.ddlCPLXBH.Items.Insert(0, new ListItem("请选择", "0"));
            this.ddlCPLXBH.Items.FindByValue(zlcp.CPLXBH).Selected = true;

            // 根据产品线
            ddlCPXBH_SelectedIndexChanged(null, null);
            this.ddlCPMCBH.Items.FindByValue(zlcp.CPMCBH).Selected = true;            
            this.ddlCPXLBH.Items.FindByValue(zlcp.CPXLBH).Selected = true;

            // 根据产品线和名称
            ddlCPMCBH_SelectedIndexChanged(null, null);
            this.ddlCPXHBH.Items.FindByValue(zlcp.CPXHBH).Selected = true;

            // 产品单位
            CS_JBDW_Dao daoDW = new CS_JBDW_Dao();
            List<CS_JBDW> lsDW = daoDW.Query4CP();
            this.ddlCPDWBH.DataSource = lsDW;
            this.ddlCPDWBH.DataValueField = "BH";
            this.ddlCPDWBH.DataTextField = "MC";
            this.ddlCPDWBH.DataBind();
            this.ddlCPDWBH.Items.Insert(0, new ListItem("请选择", "0"));
            ListItem li = this.ddlCPDWBH.Items.FindByValue(zlcp.CPDWBH);
            if(li != null)
            {
                li.Selected = true;
            }

            // 产品主颜色类型
            CS_YSLX_Dao daoCPYSLX = new CS_YSLX_Dao();
            List<CS_YSLX> lsYSLX = daoCPYSLX.QueryAll();
            this.ddlCPZYSLXBH.DataSource = lsYSLX;
            this.ddlCPZYSLXBH.DataValueField = "BH";
            this.ddlCPZYSLXBH.DataTextField = "MC";
            this.ddlCPZYSLXBH.DataBind();
            this.ddlCPZYSLXBH.Items.Insert(0, new ListItem("请选择", "0"));
            ListItem liZYSLX = this.ddlCPZYSLXBH.Items.FindByValue(zlcp.CPZYSLXBH);
            if (liZYSLX != null)
            {
                liZYSLX.Selected = true;
            }

            // 
            ddlCPZYSLXBH_SelectedIndexChanged(null, null);
            ListItem liZYS = this.ddlCPZYSBH.Items.FindByValue(zlcp.CPZYSBH);
            if (liZYS != null)
            {
                liZYS.Selected = true;
            }

            // 配色类型
            this.ddlCPPYSLXBH.DataSource = lsYSLX;
            this.ddlCPPYSLXBH.DataValueField = "BH";
            this.ddlCPPYSLXBH.DataTextField = "MC";
            this.ddlCPPYSLXBH.DataBind();
            this.ddlCPPYSLXBH.Items.Insert(0, new ListItem("请选择", "0"));
            ListItem liPYSLX =  this.ddlCPPYSLXBH.Items.FindByValue(zlcp.CPPYSLXBH);
            if (liPYSLX != null)
            {
                liPYSLX.Selected = true;
            }

            // 
            ddlCPPYSLXBH_SelectedIndexChanged(null, null);
            ListItem liPYS = this.ddlCPPYSBH.Items.FindByValue(zlcp.CPPYSBH);
            if (liPYS != null)
            {
                liPYS.Selected = true;
            }

            // 产品特性
            CS_CPTXSet_Dao daoCPTX = new CS_CPTXSet_Dao();
            List<CS_CPTX> lsCPTXX = daoCPTX.QueryAll();
            this.rbCPTX.DataSource = lsCPTXX;
            this.rbCPTX.DataValueField = "BH";
            this.rbCPTX.DataTextField = "MC";
            this.rbCPTX.DataBind();
            ListItem liCPTX = this.rbCPTX.Items.FindByValue(zlcp.CPTXBH);
            if (liCPTX != null)
            {
                liCPTX.Selected = true;
            }

            this.tbCPGG.Text = zlcp.CPGG;
            this.tbBH.Text = zlcp.BH;
            this.tbBZ.Text = zlcp.BZ;
            this.ddlCPMC.Text = zlcp.CPMC;
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            this.lbResult.Text = "";

            //  产品编码：产品编码按新的总规划思路来设计，15位，第1位为数字3。分厂-先定01
            //  家具成品("3")	分厂("01")	产品线(1)	产品类型(1)	产品名称(2)	产品系列(2)	产品型号(2)	主颜色(3) 特性(1) "-"	二级部件名称代码(3)	工艺特征(1)	三级部件或流水码(2)				
            string cpx = ddlCPXBH.SelectedValue.Trim();
            string cplx = ddlCPLXBH.SelectedValue.Trim();
            string cpmc = ddlCPMCBH.SelectedValue.Trim();
            string cpxl = ddlCPXLBH.SelectedValue.Trim();
            string cpxh = ddlCPXHBH.SelectedValue.Trim();
            string zyslx = ddlCPZYSLXBH.SelectedValue.Trim();
            string zys = ddlCPZYSBH.SelectedValue.Trim();
            string tx = rbCPTX.SelectedValue.Trim();

            string bh = "3" + "01" + cpx + cplx + cpmc + cpxl + cpxh + zyslx + zys + tx;
            this.tbBH.Text = bh;
        }

        protected void ddlCPXBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.tbBH.Text = "";
            this.lbResult.Text = "";

            this.ddlCPMCBH.Items.Clear();
            this.ddlCPXLBH.Items.Clear();
            this.ddlCPXHBH.Items.Clear();

            string cpx = this.ddlCPXBH.SelectedValue;
            if (!cpx.Equals("0"))
            {
                // 产品名称
                CS_CPMCSet_Dao daoCPMC = new CS_CPMCSet_Dao();
                List<CS_CPMC> lsCPMC = daoCPMC.QueryByCPX(cpx);
                this.ddlCPMCBH.DataSource = lsCPMC;
                this.ddlCPMCBH.DataTextField = "MC";
                this.ddlCPMCBH.DataValueField = "BH";
                this.ddlCPMCBH.DataBind();

                // 产品系列
                CS_CPXLSet_Dao daoCPXL = new CS_CPXLSet_Dao();
                List<CS_CPXL> lsCPXL = daoCPXL.QueryByCPX(cpx);
                this.ddlCPXLBH.DataSource = lsCPXL;
                this.ddlCPXLBH.DataTextField = "MC";
                this.ddlCPXLBH.DataValueField = "BH";
                this.ddlCPXLBH.DataBind();
            }
            labCPXBH.Text = this.ddlCPXBH.SelectedValue;
            this.ddlCPMCBH.Items.Insert(0, new ListItem("请选择", "0"));
            this.ddlCPXLBH.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void ddlCPMCBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.tbBH.Text = "";
            this.lbResult.Text = "";
            this.ddlCPXHBH.Items.Clear();

            string cpx = this.ddlCPXBH.SelectedValue;
            string cpmc = this.ddlCPMCBH.SelectedValue;
            if (!cpmc.Equals("0"))
            {
                // 更新产品型号
                CS_CPXHSet_Dao daoCPXH = new CS_CPXHSet_Dao();
                List<CS_CPXH> lsCPXH = daoCPXH.QueryBy(cpx, cpmc);
                this.ddlCPXHBH.DataSource = lsCPXH;
                this.ddlCPXHBH.DataTextField = "MC";
                this.ddlCPXHBH.DataValueField = "BH";
                this.ddlCPXHBH.DataBind();

            }
            this.ddlCPXHBH.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void ddlCPZYSLXBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.tbBH.Text = "";
            this.lbResult.Text = "";

            CS_YS_Dao daoCPYS = new CS_YS_Dao();
            List<CS_YS> list = daoCPYS.QueryByLX(this.ddlCPZYSLXBH.SelectedValue);
            this.ddlCPZYSBH.DataSource = list;
            this.ddlCPZYSBH.DataValueField = "BH";
            this.ddlCPZYSBH.DataTextField = "MC";
            this.ddlCPZYSBH.DataBind();
            this.ddlCPZYSBH.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void ddlCPPYSLXBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            CS_YS_Dao daoCPYS = new CS_YS_Dao();
            List<CS_YS> list = daoCPYS.QueryByLX(this.ddlCPPYSLXBH.SelectedValue);
            this.ddlCPPYSBH.DataSource = list;
            this.ddlCPPYSBH.DataValueField = "BH";
            this.ddlCPPYSBH.DataTextField = "MC";
            this.ddlCPPYSBH.DataBind();
            this.ddlCPPYSBH.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void btnSave0_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";

            string bh = this.tbBH.Text.Trim();
            if (bh.Equals(""))
            {
                this.lbResult.Text = "请生成编号";
                return;
            }

            if (this.ddlCPXBH.SelectedValue == "0")
            {
                this.lbResult.Text = "请输入产品线";
                return;
            }

            if (this.ddlCPLXBH.SelectedValue == "0")
            {
                this.lbResult.Text = "请输入产品类型";
                return;
            }

            if (this.ddlCPMCBH.SelectedValue == "0")
            {
                this.lbResult.Text = "请输入产品名称";
                return;
            }

            if (this.ddlCPXLBH.SelectedValue == "0")
            {
                this.lbResult.Text = "请输入产品系列";
                return;
            }

            if (this.ddlCPXHBH.SelectedValue == "0")
            {
                this.lbResult.Text = "请输入产品型号";
                return;
            }

            if (this.ddlCPZYSLXBH.SelectedValue == "0")
            {
                this.lbResult.Text = "请输入产品主颜色类型";
                return;
            }

            if (this.ddlCPZYSBH.SelectedValue == "0")
            {
                this.lbResult.Text = "请输入产品主颜色";
                return;
            }

            ZL_CP entity = new ZL_CP()
            {
                ID = Guid.Parse(this.hdId.Value),
                BH = bh,
                CPXBH = this.ddlCPXBH.SelectedValue,
                CPLXBH = this.ddlCPLXBH.SelectedValue,
                CPMCBH = this.ddlCPMCBH.SelectedValue,
                CPMC=this.ddlCPMC.Text.Trim(),
                CPXLBH = this.ddlCPXLBH.SelectedValue,
                CPXHBH = this.ddlCPXHBH.SelectedValue,
                CPGG = this.tbCPGG.Text,
                CPDWBH = this.ddlCPDWBH.SelectedValue,
                CPZYSLXBH = this.ddlCPZYSLXBH.SelectedValue,
                CPZYSBH = this.ddlCPZYSBH.SelectedValue,
                CPPYSLXBH = this.ddlCPPYSLXBH.SelectedValue,
                CPPYSBH = this.ddlCPPYSBH.SelectedValue,
                CPTXBH = this.rbCPTX.SelectedValue,
                BZ = this.tbBZ.Text
            };

            string resultMessage = "";
            try
            {
                ZL_CP_Dao dao = new ZL_CP_Dao();

                if (dao.Exist(bh))
                {
                    resultMessage = "同样编号的产品已存在";
                    return;
                }

                int result = dao.Update(entity);

                if (result == 1)
                {
                    resultMessage = "保存成功";
                    // ClearControlsValue();
                }
            }
            catch (Exception ex)
            {
                resultMessage = "保存失败" + ex.Message;
            }
            finally
            {
                this.lbResult.Text = resultMessage;
            }
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            initValue();
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/ZL_CP_Query.aspx");
        }
    }
}